<template>
  <vxe-modal
    :zIndex='10'
    :showFooter='true'
    title='补充信息'
    ref='modal1'
    width="800" height="300" 
    :fullscreen="false"
    >
      <div class='form-con'>
        <vxe-form  :data="infoFormData" ref='addForm' :rules="collectDataRules" size="mini" title-align="right" title-width="180" style="width: 100%">
          <vxe-form-item title="计税方法：" field="jsff" :item-render="{}">
            <template v-slot>
              <vxe-select v-model="infoFormData.jsff" placeholder="请选择" clearable>
                <vxe-option v-for="(item) in jsffList" :key="item.lxxmc" :value="item.lxxdm"
                            :label="item.lxxmc"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item>
          <vxe-form-item title="征税项目：" field="zsxm" :item-render="{}">
            <template v-slot>
              <vxe-select v-model="infoFormData.zsxm" placeholder="请选择" clearable>
                <vxe-option v-for="(item) in zsxmList" :key="item.lxxmc" :value="item.lxxdm"
                            :label="item.lxxmc"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item>
          <vxe-form-item title="税率" field="slv" :item-render="{}">
            <template v-slot>
              <vxe-select v-model="infoFormData.slv" placeholder="请选择" clearable>
                <vxe-option v-for="(item) in zslList" :key="item"  :value="item"
                            :label="item"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item>
          <vxe-form-item title="使用优惠政策：" :item-render="{}">
            <template v-slot>
              <vxe-select v-model="infoFormData.yhzcbs" placeholder="请选择" clearable>
                <vxe-option v-for="(item, index) in yhzcbsList" :key="index" 
                             :value="item.lxxdm"
                            :label="item.lxxmc"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item>

        </vxe-form>
      </div>
    <template v-slot:footer>
      <vxe-button @click="closeBox" type="reset" status="info" >取消</vxe-button>
      <vxe-button @click="save" type="submit" status="primary">保存</vxe-button>
    </template>
  </vxe-modal>


</template>
<script>
import { mixin,mixinVariable } from '@/utils/mixin.js'
import {  getCodeDetail } from '@/api/vmsClassifyCode.js'
export default {
  name: 'selectRateComp',
  mixins: [mixin,mixinVariable],
  props: {
    edit: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ifEdit: false,
      infoFormData: {
        jsff:'', //计税方法
        zsxm:'', //征税项目
        slv:'', //税率
        jzjt:'', //即征即退
        sfyz:'', //是否预征
        zcbkdk: '0', //政策不可抵扣
        lslbs: '', //零税率标识
        yhzcbs:'', //是否使用优惠政策
      },
      collectDataRules: {
        jsff: [
          { required: true, message: '请选择', trigger: 'change' },{ min: 1 },
        ],
        zsxm: [
        { required: true, message: '请选择', trigger: 'change' },{ min: 1 },

        ],
        slv: [
        { required: true, message: '请选择', trigger: 'change' },
          { min: 1 },
        ],
        sfyz: [
          { required: true, message: '请选择', trigger: 'change' },
          { min: 1 },
        ],
        zcbkdk: [
          { required: true, message: '请选择', trigger: 'change' },
          { min: 1 },
        ],
      },
      slvList:[],

      jsffList:[],
      zsxmList:[],
      zslList:[],
      zslListCache:[], // 首次的时候进行保存
      yhzcbsList:[],
    }
  },
  methods: {
    reset(){
      this.infoFormData = {
          jsff:'', //计税方法
          zsxm:'', //征税项目
          slv:'', //税率
          jzjt:'', //即征即退
          sfyz:'', //是否预征
          zcbkdk: '0', //政策不可抵扣
          yhzcbs:'', //是否使用优惠政策
      }
    },
    async save(){
       this.$refs.addForm.validate(valid=>{
          if (!valid || valid === 'undefined') {
            if(this.infoFormData.slv === '免税'){
              this.infoFormData.slv = '0%'
            }
            this.$emit('changedRate',{formdata:this.infoFormData});
            this.closeBox()
          } else {
          }
      }).then(data=>{
      },err=>{
      })

          return new Promise((resolve,reject)=>{
            resolve(1)
            reject(1)
          })

    },
    closeBox(){
      this.$refs.modal1.close()
    },
    addDW(val){
            if((val !='' && val != null&& val!= undefined)){
                try {
                    val = val * 100 + '%';
                    return (val)
                } catch (error) {
                    
                }
            }else if( val === '0' || ( val === 0)){
              return '免税'
            }
            return ''
        },
    async show(row) {
      let {data, code, message} = await getCodeDetail({
        id:  row.sphfwssflhbbm
      });
      if (code == 200) {
        this.jsffList = data.jsff; // 计税方法
        this.zsxmList = data.zsxm; // 征税项目
        this.zslList = data.zzssl; // 税率
        this.zslListCache = data.zzssl; // 税率 - 缓存
        this.yhzcbsList = data.yhzcbs; // 使用优惠政策：

        this.infoFormData.slv = (row.slv || row.slv === 0) ? this.addDW(row.slv)  : data.selectZzssl;
        this.infoFormData.jzjt = row.jzjtdm;
        this.infoFormData.jsff = row.jsff;
        this.infoFormData.zsxm = row.zsxm;
      } else {
        console.error(message);
      }
     
      this.$refs.modal1.open()
    }
  },
  watch:{
    // 'infoFormData.yhzcbs'(val){
    //   if(val == '01'){
    //     this.infoFormData.slv = '3%';
    //     this.zslList = ['3%','5%'];
    //   }else if(val == '03' || val == '04'){
    //     this.infoFormData.slv = '0%';
    //     this.zslList = ['0%'];
    //   }else if(val == '08'){
    //     this.infoFormData.slv = '3%';
    //     this.zslList = ['3%'];
    //   }else if(val == '09'){
    //     this.infoFormData.slv = '5%';
    //     this.zslList = ['5%'];
    //   }else if(val == '10'){
    //     this.infoFormData.slv = '1.5%';
    //     this.zslList = ['1.5%'];
    //   }else{
    //     this.zslList = this.zslListCache;
    //     this.infoFormData.slv = this.zslListCache[0];
    //   }
    // }
  }
}
</script>
<style lang="scss" scoped>

</style>